﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BasePlatformNetAgent;
using BasePlatformNetIO;
using System.IO;
using Newtonsoft.Json;

namespace BasePlatformClient
{
	public partial class UIForm : Form
	{
		private string version = "BasePlatformAgent Client Demo v1.25 @ 2018.4.27";

		private BasePlatformIO platformIO = null;

		private string baseJsonPipeName = @"\\.\pipe\basePipeJson";			// Json 消息管道
		private string baseJsonPipeNameLoop = @"\\.\pipe\basePipeJsonLoop";	// Json 轮询消息

		private bool sendMessageLoopEnable = false;

		public delegate void TypeLogDelegate(string str);
		public TypeLogDelegate typeLogDelegate;

		private int logNum = 0;
		private readonly int maxLogNum = 2000;

		// 构造函数
		public UIForm()
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			initEnvironment();
		}

		// 初始化运行环境
		private void initEnvironment()
		{
			this.Text = version;

			platformIO = new BasePlatformIO();
			typeLogDelegate = new TypeLogDelegate(typeLog);
			playingStateComboBox.SelectedIndex = 1;
			languageComboBox.SelectedIndex = 0;
			typeLog("Version = " + version);
		}

		// 窗体关闭前处理
		private void UIForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			platformIO.unregistPipe("iBlue");
			platformIO.unregistPipe("ledDebug");
		}

		// 打印日志
		private void typeLog(string str)
		{
			if (!logCheckBox.Checked)
			{
				return;
			}

			logRichTextBox.AppendText(str + "\r\n");
			logRichTextBox.SelectionStart = logRichTextBox.TextLength;
			logRichTextBox.ScrollToCaret();

			logNum++;
			if (logNum > maxLogNum)
			{
				logRichTextBox.Text = "";
				logNum = 0;
			}
		}

		// 清空日志
		private void clearLogButton_Click(object sender, EventArgs e)
		{
			logRichTextBox.Text = "";
		}
		
		#region iBluePlayer

		// 注册 iblue 管道
		private void registIBlueButton1_Click(object sender, EventArgs e)
		{
			string res = platformIO.registPipe("iBlue");
			this.Invoke(typeLogDelegate, "Regist iBlue Pipe, res = " + res + ", Num = " + platformIO.getIBlueCommandNum());
		}
		private void registIBlueButton2_Click(object sender, EventArgs e)
		{
			string res = platformIO.registPipe("iBlue");
			this.Invoke(typeLogDelegate, "Regist iBlue Pipe, res = " + res + ", Num = " + platformIO.getIBlueCommandNum());
		}

		// 取消注册 iblue 管道
		private void unregistIBlueButton1_Click(object sender, EventArgs e)
		{
			string res = platformIO.unregistPipe("iBlue");
			this.Invoke(typeLogDelegate, "UnRegist iBlue Pipe, res = " + res + ", Num = " + platformIO.getIBlueCommandNum());
		}
		private void unregistIBlueButton2_Click(object sender, EventArgs e)
		{
			string res = platformIO.unregistPipe("iBlue");
			this.Invoke(typeLogDelegate, "UnRegist iBlue Pipe, res = " + res + ", Num = " + platformIO.getIBlueCommandNum());
		}

		// 发送单条 iblue 测试消息
		private void testIBlueButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = platformIO.sendIBlueMessage(testIBlueTextBox.Text);
			string logStr = "[iBlue SendMessage] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}
		
		// 开始循环发送 iblue 测试消息
		private void testIBlueStartLoopSendButton_Click(object sender, EventArgs e)
		{
			sendMessageLoopEnable = true;
			Thread t = new Thread(() =>
			{
				while (sendMessageLoopEnable)
				{
					ShareMessage response = platformIO.sendIBlueMessage(testIBlueTextBox.Text);

					string logStr = "[SendMessage] Handled = " + response.Handled;
					if (response.Handled == false)
					{
						logStr += ", Reason: " + response.Value;
					}
					else
					{
						;
					}
					logStr += ", Num = " + platformIO.getIBlueCommandNum();
					this.Invoke(typeLogDelegate, logStr);

					Thread.Sleep(10);
				}
			});
			t.IsBackground = true;
			t.Start();

			testIBlueStartLoopSendButton.Enabled = false;
			testIBlueStopLoopSendButton.Enabled = true;
		}

		// 停止循环发送 iblue 测试消息
		private void testIBlueStopLoopSendButton_Click(object sender, EventArgs e)
		{
			sendMessageLoopEnable = false;
			testIBlueStopLoopSendButton.Enabled = false;
			testIBlueStartLoopSendButton.Enabled = true;
		}

		// 开始下发显示数据
		private void startSendDispDataButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;

			int x, y, w, h;
			if (Int32.TryParse(xTextBox.Text, out x) && Int32.TryParse(yTextBox.Text, out y)
				&& Int32.TryParse(wTextBox.Text, out w) && Int32.TryParse(hTextBox.Text, out h))
			{
				int fps;
				if (Int32.TryParse(fTextBox.Text, out fps) == false)
				{
					response = platformIO.startSendDispData(x, y, w, h);
				}
				else
				{
					response = platformIO.startSendDispData(x, y, w, h, fps);
				}

				string logStr = "[Start Send Data By Net] Handled = " + response.Handled;
				if (response.Handled == false)
				{
					logStr += ", Reason: " + response.Value;
				}
				else
				{
					logStr += ", Res = " + response.Value;
				}
				logStr += ", Num = " + platformIO.getIBlueCommandNum();
				this.Invoke(typeLogDelegate, logStr);
			}
			else
			{
				string logStr = "[Start Send Data By Net] Wrong Parameters!";
				this.Invoke(typeLogDelegate, logStr);
			}
		}

		// 停止下发显示数据
		private void stopSendDispDataButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.stopSendDispData();

			string logStr = "[Stop Send Data By Net] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 暂停下发显示数据
		private void pauseSendDispDataButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.pauseSendDispData();

			string logStr = "[Pause Send Data By Net] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 恢复下发显示数据
		private void resumeSendDispDataButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.resumeSendDispData();

			string logStr = "[Resume Send Data By Net] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 改变截屏区域
		private void changeCaptureAreaButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;

			int nx, ny, nw, nh;
			if (Int32.TryParse(nxTextBox.Text, out nx) && Int32.TryParse(nyTextBox.Text, out ny)
				&& Int32.TryParse(nwTextBox.Text, out nw) && Int32.TryParse(nhTextBox.Text, out nh))
			{
				response = platformIO.changeCaptureArea(nx, ny, nw, nh);

				string logStr = "[Change Capture Area] Handled = " + response.Handled;
				if (response.Handled == false)
				{
					logStr += ", Reason: " + response.Value;
				}
				else
				{
					logStr += ", Res = " + response.Value;
				}
				logStr += ", Num = " + platformIO.getIBlueCommandNum();
				this.Invoke(typeLogDelegate, logStr);
			}
			else
			{
				string logStr = "[Change Capture Area] Wrong Parameters!";
				this.Invoke(typeLogDelegate, logStr);
			}
		}

		// 改变截屏帧频
		private void changeCaptureFpsButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;

			int fps;
			if (Int32.TryParse(fpsTextBox.Text, out fps))
			{
				response = platformIO.changeCaptureFps(fps);

				string logStr = "[Change Capture Fps] Handled = " + response.Handled;
				if (response.Handled == false)
				{
					logStr += ", Reason: " + response.Value;
				}
				else
				{
					logStr += ", Res = " + response.Value;
				}
				logStr += ", Num = " + platformIO.getIBlueCommandNum();
				this.Invoke(typeLogDelegate, logStr);
			}
			else
			{
				string logStr = "[Change Capture Fps] Wrong Parameters!";
				this.Invoke(typeLogDelegate, logStr);
			}
		}

		// 获取底层设备链信息（Json格式）
		private void chainJsonButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.getDeviceChainJson();

			string logStr = "[Get Device Chain Json] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
				using(StreamWriter writer = new StreamWriter("Json-total.json"))
				{
					writer.WriteLine(response.PayloadStr);
					writer.Flush();
				}
			}
			MessageBox.Show(response.PayloadStr);
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 获取底层设备链摘要信息（Json格式）
		private void chainStatusJsonButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.getDeviceChainStatusJson();

			string logStr = "[Get Device Chain Status Json] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
				using (StreamWriter writer = new StreamWriter("Json-status.json"))
				{
					writer.WriteLine(response.PayloadStr);
					writer.Flush();
				}
			}
			MessageBox.Show(response.PayloadStr);
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 获取底层设备链摘要信息（Json格式）
		private void chainDetailJsonButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.getDeviceChainDetailJson();

			string logStr = "[Get Device Chain Detail Json] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
				using (StreamWriter writer = new StreamWriter("Json-detail.json"))
				{
					writer.WriteLine(response.PayloadStr);
					writer.Flush();
				}
			}
			MessageBox.Show(response.PayloadStr);
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 设置节目播放状态
		private void setPlayingStateButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;

			bool playing = playingStateComboBox.SelectedIndex == 0 ? true : false;
			response = platformIO.setIBluePlayingState(playing);

			string logStr = "[Set iBlue Playing State] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 查询底层硬件配置窗展开状态
		private void getConfigStateButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.getConfigExpandState();

			string logStr = "[Get Config Expand State] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				string expand = "null";
				if (response.Parameters != null && response.Parameters.Length > 0)
				{
					expand = response.Parameters[0];
					if (expand.ToLower().Equals("true"))
					{
						configStateTextBox.Text = "已展开";
					}
					else if (expand.ToLower().Equals("false"))
					{
						configStateTextBox.Text = "已关闭";
					}
					else
					{
						configStateTextBox.Text = "未知";
					}
				}
				else
				{
					configStateTextBox.Text = "未注册";
				}
				logStr += ", Res = " + response.Value + ", ConfigExpand = " + expand;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 设置节目亮度
		private void setProgramBrightButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;

			int bright;
			if (Int32.TryParse(programBrightTextBox.Text, out bright))
			{
				response = platformIO.setProgramBrightness(bright);

				string logStr = "[Set Program Bright] Handled = " + response.Handled;
				if (response.Handled == false)
				{
					logStr += ", Reason: " + response.Value;
				}
				else
				{
					logStr += ", Res = " + response.Value;
				}
				logStr += ", Num = " + platformIO.getIBlueCommandNum();
				this.Invoke(typeLogDelegate, logStr);
			}
			else
			{
				string logStr = "[Set Program Bright] Wrong Parameters!";
				this.Invoke(typeLogDelegate, logStr);
			}
		}

		// 显示系统面板
		private void showSysButton_Click(object sender, EventArgs e)
		{
			if (platformIO != null)
			{
				platformIO.showHardConfigForm();
			}
		}

		// 获取 sub 数据
		private void getSubConfigButton_Click(object sender, EventArgs e)
		{
			byte[] configSubData = new byte[128 * 1024];
			ShareMessage response = null;
			response = platformIO.getSubConfigData(ref configSubData);

			string logStr = "[Get Sub Config Data] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
				using (BinaryWriter writer = new BinaryWriter(new FileStream("Remote-Config.sub", FileMode.Create)))
				{
					writer.Write(configSubData);
					writer.Flush();
					writer.Close();
				}
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// 设置程序语言
		private void setLanguageButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;

			int language = 0;
			if (languageComboBox.SelectedIndex == 0)
			{
				language = 0;
			}
			else if (languageComboBox.SelectedIndex == 1)
			{
				language = 2;
			}
			else if (languageComboBox.SelectedIndex == 2)
			{
				language = 1;
			}

			if (language > -1 || language < 3)
			{
				response = platformIO.setUserLanguage(language);

				string logStr = "[Set User Language] Handled = " + response.Handled;
				if (response.Handled == false)
				{
					logStr += ", Reason: " + response.Value;
				}
				else
				{
					logStr += ", Res = " + response.Value;
				}
				logStr += ", Num = " + platformIO.getIBlueCommandNum();
				this.Invoke(typeLogDelegate, logStr);
			}
			else
			{
				string logStr = "[Set User Language] Wrong Parameters!";
				this.Invoke(typeLogDelegate, logStr);
			}
		}

		#endregion

		#region LedDebug

		// 注册 LedDebug 管道
		private void registLedDebugButton_Click(object sender, EventArgs e)
		{
			string res = platformIO.registPipe("LedDebug");
			this.Invoke(typeLogDelegate, "Regist LedDebug Pipe, res = " + res + ", Num = " + platformIO.getLedDebugCommandNum());
		}

		// 取消注册 LedDebug 管道
		private void unregistLedDebugButton_Click(object sender, EventArgs e)
		{
			string res = platformIO.unregistPipe("LedDebug");
			this.Invoke(typeLogDelegate, "UnRegist LedDebug Pipe, res = " + res + ", Num = " + platformIO.getLedDebugCommandNum());
		}

		// LedDebug 通信测试
		private void testLedDebugButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = platformIO.sendLedDebugMessage(testLedDebugTextBox.Text);
			string logStr = "[LedDebug SendMessage] Handled = " + response.Handled + "\n";
			logStr += JsonConvert.SerializeObject(response);
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// LedDebug 获取当前分控数量
		private void getSubNumButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.getSubControlNum();

			string logStr = "[Get Sub Control Num] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
				subNumTextBox.Text = "未注册";
			}
			else
			{
				if (response.Value.Equals("error"))
				{
					subNumTextBox.Text = "未注册";
				}
				else
				{
					string subNumText = "";
					if (response.Parameters != null)
					{
						if (response.Parameters.Length > 0)
						{
							subNumText = response.Parameters[0];
						}
						else
						{
							subNumText = "-1";
						}
						subNumTextBox.Text = subNumText;
					}
					else
					{
						subNumTextBox.Text = "返回参数为空";
					}

					logStr += ", Res = " + response.Value + ", SubNum = " + subNumText;
				}
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		// LedDebug 设置有效的分控号/端口号
		private void setEffSubAndPortButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;

			List<string> statusStrList = new List<string>();
			if (!effSubTextBox1.Text.Trim().Equals("") && !effPortTextBox1.Text.Trim().Equals(""))
			{
				statusStrList.Add(effSubTextBox1.Text.Trim() + ":" + effPortTextBox1.Text.Trim());
			}
			if (!effSubTextBox2.Text.Trim().Equals("") && !effPortTextBox2.Text.Trim().Equals(""))
			{
				statusStrList.Add(effSubTextBox2.Text.Trim() + ":" + effPortTextBox2.Text.Trim());
			}
			string[] statusStrArray = statusStrList.ToArray();

			response = platformIO.setEffectSubAndPort(statusStrArray);

			string logStr = "[Set Effect Sub and Port] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				logStr += ", Res = " + response.Value;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		#endregion

		#region 多线程测试

		private void threadTestButton1_Click(object sender, EventArgs e)
		{
			Thread t = new Thread(() =>
			{
				int num = 0;
				while (true)
				{
					num++;

					int[] addrMapType = null;
					int[] addrMap = null;
					int[] addrMapOffset = null;

					platformIO.getAddrMapTable("addrMapType", ref addrMapType);
					platformIO.getAddrMapTable("addrMap", ref addrMap);
					platformIO.getAddrMapTable("addrMapOffset", ref addrMapOffset);
					
					if (addrMapType == null)
					{
						this.Invoke(typeLogDelegate, "Thread-1: addrMapType == null");
					}
					if (addrMap == null)
					{
						this.Invoke(typeLogDelegate, "Thread-1: addrMap == null");
					}
					if (addrMapOffset == null)
					{
						this.Invoke(typeLogDelegate, "Thread-1: addrMapOffset == null");
					}

					if (addrMapType != null && addrMap != null && addrMapOffset != null)
					{
						this.Invoke(typeLogDelegate, "Thread-1: getAddrMapTable success, num = " + num);
					}

					Thread.Sleep(100);
				}
			});
			t.IsBackground = true;
			t.Start();
		}

		private void threadTestButton2_Click(object sender, EventArgs e)
		{
			Thread t = new Thread(() =>
			{
				int num = 0;
				while (true)
				{
					num++;

					int[] addrMapType = null;
					int[] addrMap = null;
					int[] addrMapOffset = null;

					platformIO.getAddrMapTable("addrMapType", ref addrMapType);
					platformIO.getAddrMapTable("addrMap", ref addrMap);
					platformIO.getAddrMapTable("addrMapOffset", ref addrMapOffset);

					if (addrMapType == null)
					{
						this.Invoke(typeLogDelegate, "Thread-2: addrMapType == null");
					}
					if (addrMap == null)
					{
						this.Invoke(typeLogDelegate, "Thread-2: addrMap == null");
					}
					if (addrMapOffset == null)
					{
						this.Invoke(typeLogDelegate, "Thread-2: addrMapOffset == null");
					}

					if (addrMapType != null && addrMap != null && addrMapOffset != null)
					{
						this.Invoke(typeLogDelegate, "Thread-2: getAddrMapTable success, num = " + num);
					}

					Thread.Sleep(100);
				}
			});
			t.IsBackground = true;
			t.Start();
		}

		private void threadTestButton3_Click(object sender, EventArgs e)
		{
			Thread t = new Thread(() =>
			{
				int num = 0;
				while (true)
				{
					num++;

					ShareMessage response = null;

					int nx, ny, nw, nh;
					if (Int32.TryParse(nxTextBox.Text, out nx) && Int32.TryParse(nyTextBox.Text, out ny)
						&& Int32.TryParse(nwTextBox.Text, out nw) && Int32.TryParse(nhTextBox.Text, out nh))
					{
						response = platformIO.changeCaptureArea(nx, ny, nw, nh);

						string logStr = "[Change Capture Area] Handled = " + response.Handled;
						if (response.Handled == false)
						{
							logStr += ", Reason: " + response.Value;
						}
						else
						{
							logStr += ", Res = " + response.Value;
						}
						logStr += ", Num = " + platformIO.getIBlueCommandNum();
						this.Invoke(typeLogDelegate, logStr);
					}
					else
					{
						string logStr = "[Change Capture Area] Wrong Parameters!";
						this.Invoke(typeLogDelegate, logStr);
					}

					Thread.Sleep(100);
				}
			});
			t.IsBackground = true;
			t.Start();
		}

		private void getSubEquActiveButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = null;
			response = platformIO.getSubEquActiveState();

			string logStr = "[Get Active State] Handled = " + response.Handled;
			if (response.Handled == false)
			{
				logStr += ", Reason: " + response.Value;
			}
			else
			{
				string active = "null";
				if (response.Parameters != null && response.Parameters.Length > 0)
				{
					active = response.Parameters[0];
					if (active.ToLower().Equals("true"))
					{
						subEquActiveTextBox.Text = "已连接";
					}
					else if (active.ToLower().Equals("false"))
					{
						subEquActiveTextBox.Text = "未连接";
					}
					else
					{
						subEquActiveTextBox.Text = "未知";
					}
				}
				else
				{
					subEquActiveTextBox.Text = "未注册";
				}
				logStr += ", Res = " + response.Value + ", Active = " + active;
			}
			logStr += ", Num = " + platformIO.getIBlueCommandNum();
			this.Invoke(typeLogDelegate, logStr);
		}

		#endregion

		#region Json/MCU

		// 设置 MCU 参数
		private void setMcuDispParameterButton_Click(object sender, EventArgs e)
		{
			int brightTotal = int.Parse(mcuBrightTotalTextBox.Text.Trim());
			int brightR = int.Parse(mcuBrightRTextBox.Text.Trim());
			int brightG = int.Parse(mcuBrightGTextBox.Text.Trim());
			int brightB = int.Parse(mcuBrightBTextBox.Text.Trim());
			int brightW = int.Parse(mcuBrightWTextBox.Text.Trim());
			int gamma = int.Parse(mcuGammaTextBox.Text.Trim());

			ShareMessage response = null;

			int bright;
			if (Int32.TryParse(programBrightTextBox.Text, out bright))
			{
				response = platformIO.setMcuDispParameter(brightTotal, brightR, brightG, brightB, brightW, gamma);

				string logStr = "[Set Mcu Disp Para] Handled = " + response.Handled;
				if (response.Handled == false)
				{
					logStr += ", Reason: " + response.Value;
				}
				else
				{
					logStr += ", Res = " + response.Value;
				}
				logStr += ", Num = " + platformIO.getIBlueCommandNum();
				this.Invoke(typeLogDelegate, logStr);
			}
			else
			{
				string logStr = "[Set Mcu Disp Para] Wrong Parameters!";
				this.Invoke(typeLogDelegate, logStr);
			}
		}

		// 获取底层连接状态 - Json
		private void getActiveStateJsonButton_Click(object sender, EventArgs e)
		{
			ShareMessage response = new ShareMessage();
			response.Handled = false;

			ShareMessage commandMsg = new ShareMessage();
			commandMsg.Type = MessageType.IBlueMessage;
			commandMsg.Value = MessageValue.IBlue_GetSubEquActiveState;

			NamedPipeNative.SendMsg(baseJsonPipeNameLoop, commandMsg, out response, true);

			if (response != null)
			{
				string logStr = "[Get ActiveState Json] Handled = " + response.Handled;
				if (response.Handled == false)
				{
					logStr += ", Reason: " + response.Value;
				}
				else
				{
					logStr += ", Res = " + response.Value + ", active = " + response.Parameters[0];
				}
				this.Invoke(typeLogDelegate, logStr);
			}
			else
			{
				this.Invoke(typeLogDelegate, "[Get ActiveState Json] response is NULL");
			}
		}

		#endregion
		
		
	}
}
